<html>
    <head>
        <title>Table Free-系统控制</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <link rel="stylesheet" media="all" href="common.css"></link>
    </head>
    <body>
        <div class="fr-fullscreen">
            <div class="fr-admin-top">
                <span class="fr-col-3 fr-align-center fr-vlign-middle"><img height="80%" src="logo.png"></span>
                <span class="fr-col-9 fr-align-center fr-txt-bold-32 fr-vlign-top headline">TableFree管理系统</span>
                <span class="fr-menu"></span>
            </div>
            <div class="fr-admin-middle">
                <div class="fr-admin-menu">
                    <ul>
                        <li><a href="network.html">网络配置</a></li>
                        <li><a href="user.html">用户管理</a></li>
                        <li class="active"><a href="javascript:;">控制端口设置</a></li>
                        <li><a href="upgrade.html">系统升级</a></li>
                        <li><a href="manual.html">使用说明书</a></li>
                    </ul>
                </div>
                <div class="fr-admin-main">
                    <div class="fr-admin-ports-control fr-align-center">
                        <div class="fr-col-10 fr-align-center">
                            <fieldset class="fr-section fr-align-left">
                            <legend>显示终端控制</legend>
                            <div class="fr-row fr-align-left" id="com_control">
                                <div class="fr-col-4 fr-row-margin5">
                                    <span class="fr-col-4 fr-align-right">波特率:</span>
                                    <span class="fr-col-8 fr-align-left">
                                        <select name="bitrate">
                                            <option value="110"> 110 </option>
                                            <option value="300"> 300 </option>
                                            <option value="600"> 600 </option>
                                            <option value="1200"> 1200 </option>
                                            <option value="2400"> 2400 </option>
                                            <option value="4800"> 4800 </option>
                                            <option value="9600"> 9600 </option>
                                            <option value="14400"> 14400 </option>
                                            <option value="19200"> 19200 </option>
                                            <option value="38400"> 38400 </option>
                                            <option value="56000"> 56000 </option>
                                            <option value="57600"> 57600 </option>
                                            <option value="115200"> 115200 </option>
                                            <option value="128000"> 128000 </option>
                                            <option value="256000"> 256000 </option>
                                        </select>
                                    </span>
                                </div>
                                <div class="fr-col-4">
                                    <span class="fr-col-4 fr-align-right">数据位:</span>
                                    <span class="fr-col-8 fr-align-left">
                                        <select name="datarate">
                                            <option value="5"> 5 </option>
                                            <option value="6"> 6 </option>
                                            <option value="7"> 7 </option>
                                            <option value="8"> 8 </option>
                                        </select>
                                    </span>
                                </div>
                                <div class="fr-col-4">
                                    <span class="fr-col-4 fr-align-right">校验位:</span>
                                    <span class="fr-col-8 fr-align-left">
                                        <select name="checkrate">
                                            <option value="NONE"> NONE </option>
                                            <option value="ODD"> ODD </option>
                                            <option value="EVEN"> EVEN </option>
                                        </select>
                                    </span>
                                </div>
                                <div class="fr-col-4">
                                    <span class="fr-col-4 fr-align-right">停止位:</span>
                                    <span class="fr-col-8 fr-align-left">
                                        <select name="stoprate">
                                            <option value="1"> 1 </option>
                                            <option value="1.5"> 1.5 </option>
                                            <option value="2"> 2 </option>
                                        </select>
                                    </span>
                                </div>
                                <div class="fr-col-4">
                                    <span class="fr-col-4 fr-align-right">硬件控制:</span>
                                    <span class="fr-col-8 fr-align-left">
                                        <select name="hardware">
                                            <option value="Rs232"> Rs232 </option>
                                            <option value="Rs485"> Rs485 </option>
                                        </select>
                                    </span>
                                </div>
                            </div>
                            <div class="fr-col-12 fr-top-dashed" id="port_control">
                                <div class="fr-row fr-align-left">
                                    <div class="fr-col-10 fr-row-margin5">
                                        <span class="fr-col-4 fr-align-right"><b></b></span>
                                        <span class="fr-col-5 fr-align-left"></span>
                                    </div>
                                    <div class="fr-col-10 fr-row-margin5">
                                        <span class="fr-col-4 fr-align-right">显示开:</span>
                                        <span class="fr-col-5 fr-align-left"><input type="text" name="uartdisplayOn" value=""></span>
                                        <span class="fr-col-3 fr-align-left">
                                            <span class="fr-btn fr-btn-gray clearbtn" data-prefix="uart" data-target="displayOn">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-prefix="uart" data-target="displayOn">测试</span>
                                        </span>
                                    </div>
                                    <div class="fr-col-10 fr-row-margin5">
                                        <span class="fr-col-4 fr-align-right">显示关:</span>
                                        <span class="fr-col-5 fr-align-left"><input type="text" name="uartdisplayOff" value=""></span>
                                        <span class="fr-col-3 fr-align-left">
                                            <span class="fr-btn fr-btn-gray clearbtn" data-prefix="uart" data-target="displayOff">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-prefix="uart" data-target="displayOff">测试</span>
                                        </span>
                                    </div>
                                    <div class="fr-col-10 fr-row-margin5">
                                        <span class="fr-col-4 fr-align-right">输入通道选择:</span>
                                        <span class="fr-col-5 fr-align-left"><input type="text" name="uartchannelChoose" value=""></span>
                                        <span class="fr-col-3 fr-align-left">
                                            <span class="fr-btn fr-btn-gray clearbtn" data-prefix="uart" data-target="channelChoose">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-prefix="uart" data-target="channelChoose">测试</span>
                                        </span>
                                    </div>
                                </div>
                            </div>
                          </fieldset>
                        </div>
                        <div class="fr-col-10 fr-align-center fr-row-margin5" id="ir_control">
                            <fieldset class="fr-section fr-align-left">
                            <legend>IR学习与控制</legend>
                                <div class="fr-row fr-align-left fr-col-8">
                                    <div class="fr-row-margin5">
                                        <span class="fr-col-6 fr-align-right">显示开:</span>
                                        <span class="fr-col-6 fr-align-left">
                                            <span class="fr-circle fr-bg-black fr-col-margin10"></span>
                                            <input type="hidden" name="IRdisplayOn" value="" />
                                            <span class="fr-btn fr-btn-gray studybtn" data-target="displayOn" data-prefix="IR">学习</span>
                                            <span class="fr-btn fr-btn-gray clearbtn" data-target="displayOn" data-prefix="IR">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-target="displayOn" data-prefix="IR">测试</span>
                                        </span>
                                    </div>
                                    <div class="fr-row-margin5">
                                        <span class="fr-col-6 fr-align-right">显示关:</span>
                                        <span class="fr-col-6 fr-align-left">
                                            <span class="fr-circle fr-bg-black fr-col-margin10"></span>
                                            <input type="hidden" name="IRdisplayOff" value="" />
                                            <span class="fr-btn fr-btn-gray studybtn" data-target="displayOff" data-prefix="IR">学习</span>
                                            <span class="fr-btn fr-btn-gray clearbtn" data-target="displayOff" data-prefix="IR">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-target="displayOff" data-prefix="IR">测试</span>
                                        </span>
                                    </div>
                                    <div class="fr-row-margin5">
                                        <span class="fr-col-6 fr-align-right">输入通道选择:</span>
                                        <span class="fr-col-6 fr-align-left">
                                            <span class="fr-circle fr-bg-black fr-col-margin10"></span>
                                            <input type="hidden" name="IRchannelChoose" value="" />
                                            <span class="fr-btn fr-btn-gray studybtn" data-target="channelChoose" data-prefix="IR">学习</span>
                                            <span class="fr-btn fr-btn-gray clearbtn" data-target="channelChoose" data-prefix="IR">清除</span>
                                            <span class="fr-btn fr-btn-gray testbtn" data-target="channelChoose" data-prefix="IR">测试</span>
                                        </span>
                                    </div>
                                </div>
                                <div class="fr-col-4 fr-align-left fr-vlign-top">
                                    <p><b>说明:</b></p>
                                    <p class="fr-color-gray">1. 灯黑色常量表示未学习红外码</p>
                                    <p class="fr-color-gray">2. 灯绿色常量表示已学习红外码</p>
                                    <p class="fr-color-gray">3. 灯绿色闪烁表示正学习红外码</p>
                                    <p class="fr-color-gray">4. 灯红色常量表示学习红外码失败</p>
                                </div>
                          </fieldset>
                        </div>
                        <div class="fr-row fr-col-10" id="system_control">
                            <div class="fr-col-12">
                                <div class="fr-col-6 fr-row-margin5">
                                    <span class="fr-col-5 fr-align-right">系统自动关机时间:</span>
                                    <span class="fr-col-7 fr-align-left">
                                        <input type="text" name="system-off" data-max="60" value="" style="width: 50px;"> 分(min)
                                    </span>
                                    <span class="fr-col-5 fr-align-right"></span>
                                    <span class="fr-col-7 fr-align-left fr-color-gray">范围:0~60,0为不自动关机</span>
                                </div>
                                <div class="fr-col-6 fr-row-margin5">
                                    <span class="fr-col-5 fr-align-right">开机延时:</span>
                                    <span class="fr-col-7 fr-align-left">
                                        <input type="text" name="auto-off" data-max="120" value="" style="width: 50px;"> 秒(sec)
                                    </span>
                                    <span class="fr-col-5 fr-align-right"></span>
                                    <span class="fr-col-7 fr-align-left fr-color-gray">范围:0~120</span>
                                </div>
                            </div>
                            <div class="fr-col-12">
                                <div class="fr-col-6 fr-row-margin5">
                                    <span class="fr-col-5 fr-align-right">HDMI输出格式:</span>
                                    <span class="fr-col-7 fr-align-left">
                                        <select name="output-format">
                                        </select>
                                    </span>
                                </div>
                                <div class="fr-col-6 fr-row-margin5">
                                    <span class="fr-col-5 fr-align-right"></span>
                                    <span class="fr-col-7 fr-align-left">
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div class="fr-row fr-col-12 fr-align-center" id="control_btns">
                            <div class="fr-row fr-col-4">
                                <span class="fr-btn fr-btn-gray" id="savehw">参数上传</span>
                            </div>
                            <div class="fr-row fr-col-4">
                                <span class="fr-btn fr-btn-gray" id="downhw">配置另存</span>
                            </div>
                            <div class="fr-row fr-col-4">
                                <span class="fr-btn fr-btn-gray" id="readhw">打开配置</span>
                                <input type="file" name="file" id="file" value="" class="fr-hide">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="fr-clear"></div>
            </div>
            <div class="fr-admin-bottom fr-align-center" id="status_bar">
                <span class="fr-col-3"><i class="fa"></i> <span id="status_ip">IP:192.168.1.2</span></span>
                <span class="fr-col-2"><i class="fa"></i> <span id="status_text">Online</span></span></span>
                <span class="fr-col-2"><i class="fa"></i> <span id="status_username"></span></span>
                <span class="fr-col-3"><i class="fa"></i> <span id="status_time"></span></span>
            </div>
        </div>
        <div class="fr-container fr-align-center fr-fullscreen fr-mask" id="mask">
            <div class="fr-table fr-fullscreen">
                <div class="fr-table-cell fr-vlign-middle">
                    <ul class="cur-login-box fr-col-4">
                        <li class="fr-row-40 fr-row-title fr-align-left">系统提醒</li>
                        <li class="fr-popup-content">
                            红外学习中，点击确定按钮停止
                        </li>
                        <li class="fr-row-40 fr-align-center">
                            <span class="fr-btn fr-btn-orange">确定</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        </div>
        <script type="text/javascript" src="jquery.1.11.3.mini.js"></script>
        <script type="text/javascript" src="common.js"></script>
        <script type="text/javascript">
            var handle = {}, result = {}, clickable = true, stopQuery = false;
            $(document).ready(function(){
                // $('#readhw').on('click', function(){
                //     var idx = $('select[name=hardware]').val();
                //     request('/uart.cgi', {index: idx},'get', function(data){
                //         $('select[name=bitrate]').val(data.baudrate);
                //         $('select[name=datarate]').val(data.wordWidth);
                //         $('select[name=stoprate]').val(data.stopBits);
                //         $('select[name=checkrate]').val(data.checkType);
                //     })
                // })
                var irmap = {
                            IRdisplayOn: 'IRCtrlDpOn', 
                            IRdisplayOff: 'IRCtrlDpOff', 
                            IRchannelChoose: 'IRCtrlChSel',
                        };
                var uartmap = {
                    uartdisplayOn: 'uartCtrlDpOn', 
                    uartdisplayOff: 'uartCtrlDpOff', 
                    uartchannelChoose: 'uartCtrlChSel',
                }
                $('#savehw').on('click', function(){
                    init.onSave();
                })
                var queryStudyStatus = function(url, param, target){
                    if(stopQuery){
                        stopQuery = false;
                        return;
                    }
                    request(url , param, 'get', function(ret){
                        clickable = true;
                        if(ret.retCode == 100){
                            if(handle[target]){
                                handle[target].stop('green');
                                handle[target] = null;
                            }
                            $('#mask').fadeOut();
                            clickable = true;
                            return popup.toast('学习红外码完成');
                        }
                        if(ret.retCode == 101 || ret.retCode == 0){
                            setTimeout(function(){
                                queryStudyStatus(url, param, target);
                            }, 1000);
                            return;
                        }
                        if(ret.retCode == 102){
                            if(handle[target]){
                                handle[target].stop('red');
                                handle[target] = null;
                            }
                            $('#mask').fadeOut();
                            clickable = true;
                            return popup.toast('学习红外码失败');
                        }
                        if(ret.retCode == 103){
                            if(handle[target]){
                                handle[target].stop('red');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('学习红外码超时');
                        }
                        if(ret.retCode == 104){
                            if(handle[target]){
                                handle[target].stop('black');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('未学习红外码');
                        }
                        return popup.toast('无法识别的返回信息');
                    }, function(){
                        
                    }, function(){
                        if(handle[target]){
                            handle[target].stop('red');
                            handle[target] = null;
                        }
                        $('#mask').fadeOut();
                        clickable = true;
                        return popup.toast('学习失败');
                    }, function(){

                    },true)
                }
                //清除
                $('.clearbtn').on('click', function(){
                    var target = $(this).data('target');
                    var prefix = $(this).data('prefix');
                    var params = {action: 'clean'};
                    var urls   = prefix == 'IR' ? irmap : uartmap;
                    var url = urls[prefix+target];
                    if(!url){
                        return popup.toast('参数错误，找不到请求URL');
                    }
                    request('/' + url + '.cgi', params,'post', function(){
                        return popup.toast("清除成功");
                    })
                })
                //测试
                $('.testbtn').on('click', function(){
                    var target = $(this).data('target');
                    var prefix = $(this).data('prefix');
                    var params = {action: 'test'};
                    var urls   = prefix == 'IR' ? irmap : uartmap;
                    if(prefix != 'IR'){
                        params['cmd'] = $('input[name=' + prefix + target + ']').val();
                    }
                    var url = urls[prefix + target];
                    if(!url){
                        return popup.toast('参数错误，找不到请求URL');
                    }
                    request('/' + url + '.cgi', params,'post', function(){
                        return popup.toast("测试完成");
                    })
                })
                //学习
                $('.studybtn').on('click', function(){
                    if(!clickable){
                        return popup.toast("当前操作未完成，请稍后操作");
                    }
                    clickable = false;
                    var target = $(this).data('target');
                    var prefix = $(this).data('prefix');
                    var params = {action: 'study'};
                    var url    = irmap[prefix + target];
                    if(!url){
                        return popup.toast('参数错误，找不到请求URL');
                    }
                    var o = $(this).parents('span').find('.fr-circle');
                        handle[target] = new lighter();
                        handle[target].flicker(o, 'green');
                    request('/' + url + '.cgi', params, 'post', function(ret){
                        //学习中
                        if(ret && (ret.retCode == 101 || ret.retCode == 0)){
                            popup.alert('<div class="fr-row-40">学习中，点击确定取消</div>', function(){
                                request('/' + url + '.cgi', {action: 'stop'}, 'post', function(){
                                    clickable = true;
                                    if(handle[target]){
                                        handle[target].stop('black');
                                        handle[target] = null;
                                    }
                                    stopQuery = true;
                                })
                            });
                            setTimeout(function(){
                                queryStudyStatus.call(o,'/' + url + '.cgi', params, target);
                            }, 1000);
                            return;
                        }
                        if(ret && ret.retCode == 100){
                            if(handle[target]){
                                handle[target].stop('green');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('学习红外码完成');
                        }
                        if(ret.retCode == 102){
                            if(handle[target]){
                                handle[target].stop('red');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('学习红外码失败');
                        }
                        if(ret.retCode == 103){
                            if(handle[target]){
                                handle[target].stop('red');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('学习红外码超时');
                        }
                        if(ret.retCode == 104){
                            if(handle[target]){
                                handle[target].stop('black');
                                handle[target] = null;
                            }
                            clickable = true;
                            $('#mask').fadeOut();
                            return popup.toast('学习红外码完成');
                        }
                        return popup.toast('无法识别的返回信息');
                    }, function(){

                    }, function(){
                        clickable = true;
                        if(handle[target]){
                            handle[target].stop('red');
                            handle[target] = null;
                        }
                        $('#mask').fadeOut();
                        return popup.toast('学习失败');
                    },true)
                })
                $('#downhw').on('click', function(){
                    var data = {};
                    $('input[type=text],select,:hidden').each(function(){
                        var key = $(this).attr('name');
                        var val = $(this).val();
                        data[key] = val;
                    })
                    var savedData = {
                        hwCfg: {
                            boundrate: data.bitrate,
                            wordWidth: data.datarate,
                            checkType: data.checkrate,
                            stopBits: data.stoprate,
                            hwType: data.hardware,
                        },
                        irCfg:{
                            displayOn: JSON.parse(data.IRdisplayOn),
                            displayOff: JSON.parse(data.IRdisplayOff),
                            channelChoose: JSON.parse(data.IRchannelChoose),
                        },
                        uartCfg: {
                            displayOn: data.uartdisplayOn,
                            displayOff: data.uartdisplayOff,
                            channelChoose: data.uartchannelChoose,
                        },
                        hdmiCfg: {
                            autoPowerDown: data["auto-off"],
                            sysDelayOn: data["system-off"],
                            hdmiOutMode: data["output-format"],
                        }
                    };
                    init.onDownload(savedData);
                })
                $('#file').on('change', function(){
                    init.onRead.call(this);
                })
                $('#readhw').on('click', function(){
                    $('#file').click();
                })

                var showSaveResult = function(){
                    var total   = 8;
                    var current = 0;
                    var flag    = true;
                    for(var i in result){
                        current++;
                        flag &= result[i];
                    }
                    if(current == total){
                        return popup.toast(flag ? '配置保存成功' : '配置保存失败');
                    }
                }
                var init = {
                    flags:{port: 0, ir: 0},
                    ir: function(){
                        var _this = this;
                        var items   = {
                                    IRCtrlDpOn: 'IRdisplayOn',
                                    IRCtrlDpOff: 'IRdisplayOff',
                                    IRCtrlChSel: 'IRchannelChoose'
                                    }
                        for(var i in items){
                            (function(intf, eleName){
                                request('/' + intf + '.cgi', {}, 'get', function(data){
                                    //完成之后再请求红外学习的接口
                                    if(++_this.flags.ir >= Object.keys(items).length){
                                        _this.port();
                                    }
                                    var cmd = typeof(data.cmd) == 'string' ? data.cmd : JSON.stringify(data.cmd);
                                    $('input[name=' + eleName + ']').val(cmd)
                                }, function(){
                                    popup.toast('请求服务器失败');
                                    if(++_this.flags.ir >= items.length){
                                        _this.port();
                                    }
                                })
                            })(i, items[i])
                        }
                    },
                    port: function(){
                        var items = {
                                    uartCtrlDpOn:'uartdisplayOn', 
                                    uartCtrlDpOff:'uartdisplayOff', 
                                    uartCtrlChSel:'uartchannelChoose'
                                    };
                        for(var i in items){
                            (function(intf, eleName){
                                request('/' + intf + '.cgi', {}, 'get', function(data){
                                    $('input[name=' + eleName + ']').val(data.cmd)
                                })
                            })(i, items[i])
                        }
                    },
                    hardware: function(){
                        var _this = this;
                        request('/uart.cgi?index=0', null, 'get', function(data){
                            var bitrate = '', wordWidth = '', stopBits = '', checkType = '', hwType = '';
                            for(var i in data.baudrateList){
                                var selected = data.baudrateList[i] == data.baudrate ? ' selected' : '';
                                bitrate += '<option value="' + data.baudrateList[i] + '"' + selected + '>' + data.baudrateList[i] + '</option>';
                            }
                            for(var i in data.wordWidthList){
                                var selected = data.wordWidthList[i] == data.wordWidth ? ' selected' : '';
                                wordWidth += '<option value="' + data.wordWidthList[i] + '"' + selected + '>' + data.wordWidthList[i] + '</option>';
                            }
                            for(var i in data.stopBitsList){
                                var selected = data.stopBitsList[i] == data.stopBits ? ' selected' : '';
                                stopBits += '<option value="' + data.stopBitsList[i] + '"' + selected + '>' + data.stopBitsList[i] + '</option>';
                            }
                            for(var i in data.checkTypeList){
                                var selected = data.checkTypeList[i] == data.checkType ? ' selected' : '';
                                checkType += '<option value="' + data.checkTypeList[i] + '"' + selected + '>' + data.checkTypeList[i] + '</option>';
                            }
                            for(var i in data.hwTypeList){
                                var selected = data.hwTypeList[i] == data.hwType ? ' selected' : '';
                                hwType += '<option value="' + data.hwTypeList[i] + '"' + selected + '>' + data.hwTypeList[i] + '</option>';
                            }
                            $('select[name=bitrate]').html(bitrate);
                            $('select[name=datarate]').html(wordWidth);
                            $('select[name=stoprate]').html(stopBits);
                            $('select[name=checkrate]').html(checkType);
                            $('select[name=hardware]').html(hwType);
                            _this.ir();
                        });
                    },
                    hdmiout: function(){
                        request('/sysCtrl.cgi', null, 'get', function(data){
                            $('input[name=system-off]').val(data.sysDelayOn);
                            $('input[name=auto-off]').val(data.autoPowerDown);
                            var hdmihtml = '';
                            for(var i in data.hdmiOutModeList){
                                var thisMode = data.hdmiOutModeList[i];
                                var selected = thisMode == data.hdmiOutMode ? ' selected' : '';
                                hdmihtml += '<option' + selected + ' value="' + thisMode + '">' + thisMode + '</option>';
                            }
                            $('select[name=output-format]').html(hdmihtml);
                        })
                    },
                    onInput: function(){
                        $('input[name=auto-off], input[name=system-off]').on('keydown', function(e){
                            return (e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode == 8 || (e.keyCode >= 37 && e.keyCode <= 40);
                        });
                        $('input[name=auto-off], input[name=system-off]').on('input', function(){
                            var max = $(this).data('max');
                            if($(this).val() > max){
                                $(this).val(max);
                            }
                        })
                    },
                    //读取配置
                    onRead: function(){
                        if(this.files.length == 0){
                            return;
                        }
                        if(!FileReader){
                            return popup.toast("您使用的浏览器版本太低，请升级，推荐使用谷歌浏览器");
                        }
                        var fr = new FileReader();
                        fr.onload = function(){
                            var content = this.result;
                            var config  = JSON.parse(content);
                            if(!config){
                                return  popup.toast('数据格式不正确，无法解析配置参数');
                            }
                            console.log(config);
                            for(var i in config.hwCfg){
                                var fields = {
                                            'boundrate': "bitrate", 
                                            'wordWidth': 'datarate', 
                                            'checkType': 'checkrate',
                                            'stopBits': 'stoprate',
                                            'hwType': 'hardware'
                                            };
                                $('[name=' + fields[i] + ']').val(config.hwCfg[i]);
                            }

                            for(var i in config.uartCfg){
                                $('[name=uart' + i + ']').val(config.uartCfg[i]);
                            }

                            for(var i in config.irCfg){
                                $('[name=IR' + i + ']').val(JSON.stringify(config.irCfg[i]));
                            }

                            for(var i in config.hdmiCfg){
                                var fields = {
                                    'autoPowerDown': 'auto-off',
                                    'sysDelayOn': 'system-off',
                                    'hdmiOutMode': 'output-format'
                                };
                                $('[name=' + fields[i] + ']').val(config.hdmiCfg[i]);
                            }
                        }
                        fr.readAsText(this.files[0]);
                    },
                    //保存配置到本地文件
                    onDownload: function(data){
                        if(!URL || !Blob){
                            return popup.toast("您使用的浏览器版本太低，请升级，推荐使用谷歌浏览器");
                        }
                        var id = 'downTag';
                        var content = new Blob([JSON.stringify(data)]);
                        var src = URL.createObjectURL(content);
                        var downTag = document.createElement('a');
                        downTag.download = "配置.json";
                        downTag.href = src;
                        document.body.appendChild(downTag);
                        downTag.click();
                        document.body.removeChild(downTag);
                    },
                    savePort: function(){
                        var _this = this;
                        for(var i in uartmap){
                            (function(intf, eleName){
                                var fieldValue = $('input[name=' + eleName + ']').val();
                                var data    = {action: 'set', cmd: fieldValue};
                                request('/' + intf + '.cgi', data, 'post', function(){
                                    result[eleName] = true;
                                    showSaveResult();
                                }, function(){
                                    result[eleName] = false;
                                    showSaveResult();
                                })
                            })(uartmap[i], i)
                        }
                    },
                    saveIr: function(){
                        //保存红外配置
                        for(var i in irmap){
                            (function(intf, eleName){
                                var fieldValue = $('input[name=' + eleName + ']').val();
                                var data    = {action: 'set'};
                                request('/' + intf +'.cgi', data, 'post', function(){
                                    result[eleName] = true;
                                    showSaveResult();
                                },function(){
                                    result[eleName] = false;
                                    showSaveResult();
                                })
                            })(irmap[i], i)
                        }
                    },
                    //保存配置到服务器
                    onSave: function(){
                        var _this = this;
                        //保存HDMI配置
                        var data = {
                            sysDelayOn: $('input[name=system-off]').val(),
                            autoPowerDown: $('input[name=auto-off]').val(),
                            hdmiOutMode: $('select[name=output-format]').val(),
                        };
                        request('/sysCtrl.cgi', data, 'post', function(){
                            result.hdmi = true;
                            showSaveResult();
                            // return popup.toast('保存成功');
                        }, function(){
                            result.hdmi = false;
                            showSaveResult();
                        });
                        //保存硬件参数配置
                        var data = {
                            index:0,
                            baudrate: $('select[name=bitrate]').val(),
                            wordWidth: $('select[name=datarate]').val(),
                            checkType: $('select[name=checkrate]').val(),
                            stopBits: $('select[name=stoprate]').val(),
                            hwType: $('select[name=hardware]').val(),
                        };
                        request('/uart.cgi?index=0', data, 'post', function(){
                            result.hardware = true;
                            showSaveResult();
                            _this.savePort();
                        }, function(){
                            result.hardware = false;
                            showSaveResult();
                        })
                        
                    }
                };

                init.hardware();
                init.hdmiout();
                init.onInput();
            })
        </script>
    </body>
    </html>